package com.homeparking.manage.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.homeparking.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.homeparking.common.annotation.Log;
import com.homeparking.common.core.controller.BaseController;
import com.homeparking.common.core.domain.AjaxResult;
import com.homeparking.common.enums.BusinessType;
import com.homeparking.manage.domain.ManageCommunity;
import com.homeparking.manage.service.IManageCommunityService;
import com.homeparking.common.utils.poi.ExcelUtil;
import com.homeparking.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 小区信息Controller
 * 
 * @author liuyangyang
 * @date 2025-10-23
 */
@RestController
@RequestMapping("/manage/manage_community")
public class ManageCommunityController extends BaseController
{
    @Autowired
    private IManageCommunityService manageCommunityService;

    /**
     * 查询小区信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ManageCommunity manageCommunity)
    {
        startPage();
        List<ManageCommunity> list = manageCommunityService.selectManageCommunityList(manageCommunity);
        return getDataTable(list);
    }

    @Log(title = "小区管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('manage:community:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ManageCommunity> util = new ExcelUtil<ManageCommunity>(ManageCommunity.class);
        List<ManageCommunity> communityList = util.importExcel(file.getInputStream());
        String message = manageCommunityService.importCommunity(communityList, updateSupport);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<ManageCommunity> util = new ExcelUtil<ManageCommunity>(ManageCommunity.class);
        util.importTemplateExcel(response, "小区数据");
    }


    /**
     * 导出小区信息列表
     */
    @PreAuthorize("@ss.hasPermi('manage:manage_community:export')")
    @Log(title = "小区信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ManageCommunity manageCommunity)
    {
        List<ManageCommunity> list = manageCommunityService.selectManageCommunityList(manageCommunity);
        ExcelUtil<ManageCommunity> util = new ExcelUtil<ManageCommunity>(ManageCommunity.class);
        util.exportExcel(response, list, "小区信息数据");
    }

    /**
     * 拉取小区信息
     */
    @PostMapping("/pull")
    public AjaxResult pullManageCommunity(@RequestBody List<ManageCommunity> manageCommunity)
    {
        return toAjax(manageCommunityService.pullManageCommunity(manageCommunity));
    }

    /**
     * 获取小区信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:manage_community:query')")
    @GetMapping(value = "/{communityId}")
    public AjaxResult getInfo(@PathVariable("communityId") String communityId)
    {
        return success(manageCommunityService.selectManageCommunityByCommunityId(communityId));
    }

    /**
     * 新增小区信息
     */
    @PreAuthorize("@ss.hasPermi('manage:manage_community:add')")
    @Log(title = "小区信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ManageCommunity manageCommunity)
    {
        return toAjax(manageCommunityService.insertManageCommunity(manageCommunity));
    }

    /**
     * 修改小区信息
     */
    @PreAuthorize("@ss.hasPermi('manage:manage_community:edit')")
    @Log(title = "小区信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ManageCommunity manageCommunity)
    {
        return toAjax(manageCommunityService.updateManageCommunity(manageCommunity));
    }

    /**
     * 删除小区信息
     */
    @PreAuthorize("@ss.hasPermi('manage:manage_community:remove')")
    @Log(title = "小区信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{communityIds}")
    public AjaxResult remove(@PathVariable String[] communityIds)
    {
        return toAjax(manageCommunityService.deleteManageCommunityByCommunityIds(communityIds));
    }
}
